/* ===========================================================================
 *
 *       Filename:  xyb_handwriting.h
 *
 *    Description:  handwriting zh_CN, xyb (qt)
 *
 * - Mode: 644
 * - NOTE. NOT use "dos"
 * - Updated: 2015-04-26 16:42:31
 *
 *        Version:  1.0.0
 *        Created:  2015-04-22
 *       Revision:  1.0.0
 *       Compiler:  moc/g++
 *
 *         Author:  Yui Wong, email: yuiwong@126.com
 *   Organization:  ""
 *        License:  LGPLv3
 *
 *  This file is part of portable-inputmethod.yw.
 *
 *  portable-inputmethod.yw
 *  is free software: you can redistribute it
 *  and/or modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  portable-inputmethod.yw
 *  is distributed in the hope that it will be
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with portable-inputmethod.yw.
 *  If not, see <http://www.gnu.org/licenses/>.
 *
 * ===========================================================================
 */


#if !defined(_XYB_HANDWRITING_H_)
#define _XYB_HANDWRITING_H_ (1)


#include <stdint.h>
#include <xdll.h>

#if !defined(ID_QT_VERSION)
#	error "ID_QT_VERSION NOT defined"
#endif

#include <QtCore/QObject>
#include <QtCore/QTimer>
#include <QtCore/QQueue>
#include <QtGui/QMouseEvent>
#include <QtGui/QPaintEvent>

#if (ID_QT_VERSION < 0x050000)
#	include <QtGui/QWidget>
#	include <QtGui/QPushButton>
#	include <QtGui/QHBoxLayout>
#	include <QtGui/QVBoxLayout>
#else
#	include <QtWidgets/QWidget>
#	include <QtWidgets/QPushButton>
#	include <QtWidgets/QHBoxLayout>
#	include <QtWidgets/QVBoxLayout>
#endif

#include <xyb_handwriting_manager.h>


#define XYB_HW_CB_NUM (10) /* max */


class xyb_handwriting: public inputmethod {
	Q_OBJECT

friend class xyb_handwriting_manager;

/*
 * INTERFACE(S)
 */
signals: void char_chose(const QWidget * focus,
	const char * utf8);/* 向其他窗口传递 utf8 cstr */


protected: xyb_handwriting(QWidget * parent = 0);
protected: virtual int reinitialize_core(QWidget * focus,
	const char * mf = 0);
public: ~xyb_handwriting(void);


private: static char sn[]; /* series number */
private: static const enum ae_t { /* application environment */
	ae_non_embedded = 0,
	ae_embedded = 1,
} ae;
private: static char mf[];/* model file */
private: static const int recg_flag;
private: static const int recg_charset_ids[];
private: static int candidate_charcodes[];
private: static int candidate_charcodes_pc[];
private: static const int max_candidate_count;


private: void do_deinit_core(void);
private: void CORE_READY(void);
private: bool core_initialized;
private: uint8_t access;

private: static size_t writor_width_fixed;
private: static size_t writor_height_fixed;
private: static int writor_pen_w_fixed;
private: QTimer recogtimer;
private: QWidget * focus;

/*
 * ---------------------------------------------------------------------------
 * xyb recognize parts
 * ---------------------------------------------------------------------------
 */
private: QQueue<short> pnts_c;/* which column -- [0 ~ (width - 1)] -- x */
private: QQueue<short> pnts_r;/* which row -- [0 ~ (height - 1)] -- y */
/* end-flag if pnts[i] is end => 0x0001, else: 0 */
private: QQueue<short> pnts_e;
private: int candidate_count;
/*
 * ---------------------------------------------------------------------------
 * end of xyb recognize parts
 * ---------------------------------------------------------------------------
 */


private: void mouseMoveEvent(QMouseEvent * evt);
private: void mousePressEvent(QMouseEvent * evt);
private: void mouseReleaseEvent(QMouseEvent * evt);
private: void paintEvent(QPaintEvent * evt);

private: void genbuttonstate(void);
private: inputmethod_manager::click_state_t mouse_state;

private slots: void recognize(void);
private slots: void turnpageup(void);
private slots: void turnpagedown(void);
private slots: void choose_char(void);

private: QVBoxLayout * master_layout;
private: QHBoxLayout * writor_layout;
private: QVBoxLayout * option_layout;
private: QHBoxLayout * candidate_layout;
private: QPushButton * candidate_btns[XYB_HW_CB_NUM];
private: QPushButton * up;
private: QPushButton * down;
private: bool is_self(const QWidget * w);
private slots: void restore_last_focus(QWidget * oldf, QWidget * newf);
private: int index;
private: int allpage;
}; /* class xyb_handwriting */


#endif /* !defined(_XYB_HANDWRITING_H_) */


/*
 * end of file xyb_handwriting.h
 */
